package cn.com.pxto.service.impl;

import java.util.List;
import java.util.Vector;

import cn.com.pxto.commons.SearchParam;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.ContactDAO;
import cn.com.pxto.dao.GuestDAO;
import cn.com.pxto.dao.LessonDAO;
import cn.com.pxto.dao.SchoolDAO;
import cn.com.pxto.dao.UserDAO;
import cn.com.pxto.model.Contact;
import cn.com.pxto.model.Guest;
import cn.com.pxto.model.InvalidGuest;
import cn.com.pxto.model.User;
import cn.com.pxto.service.GuestManager;

public class GuestManagerImpl implements GuestManager {
	private GuestDAO guestDAO;
	private UserDAO userDAO;
	private ContactDAO contactDAO;
	private SchoolDAO schoolDAO;
	private LessonDAO lessonDAO;

	@Override
	public void createGuest(Guest guest) {
		Integer signType = guest.getSignType();
		if(signType == 0){
//		已注册用户保存为Contact
			Contact contact = new Contact(guest);
			contactDAO.saveContact(contact);
		}else{
//		未注册用户保存为guest
			guestDAO.saveGuest(guest);
		}
	}

	@Override
	public void deleteGuest(int id) {
		guestDAO.deleteGuest(id);

	}

	@Override
	public Guest getGust(int id) {
		return guestDAO.getGuest(id);
	}

	@Override
	public List<Guest> listGuest(int userId,String sort, PageInfo pageInfo) {
		User user=userDAO.getUser(userId);
		if(user.getType() == 2){
			return guestDAO.listGuestForManager(sort, pageInfo);
		}else {
			return guestDAO.listGuestForNormal(userId, sort, pageInfo);
		}
	}
	/**
	 * Object[startTime,endTime,userId,guestName,schoolName,lessonName,usesrName];
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Guest> getGuest(String sort,Object[] o, PageInfo pageInfo) {
		Vector paraVct = new Vector();
		if(o[0]!=null){
			paraVct.add(new SearchParam("logonDate",o[0],SearchParam.SEARCH_PARAM_TYPE_GREATER_OR_EQUAL));
		}
		if(o[1]!=null){
			paraVct.add(new SearchParam("logonDate",o[1],SearchParam.SEARCH_PARAM_TYPE_LESS_OR_EQUAL));
		}
		if(o[2]!=null){
			User user=userDAO.getUser((Integer)o[2]);
			if(user.getType() != 2){
				paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));
			}	
		}
		if(o[3]!=null){
			paraVct.add(new SearchParam("name",o[3],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		if(o[4]!=null){
			List<Integer> shoolIdList=schoolDAO.getSchoolByName((String)o[4]);
			if(shoolIdList!=null && shoolIdList.size()>0){
				StringBuffer strBuf=new StringBuffer();
				for (int i = 0; i < shoolIdList.size(); i++) {
					Integer id=shoolIdList.get(i);
					strBuf.append(id);
					strBuf.append(",");
				}
				strBuf.deleteCharAt(strBuf.length() -1);
				paraVct.add(new SearchParam("schoolId",strBuf.toString(),SearchParam.SEARCH_PARAM_TYPE_IN));
			}
		}
		if(o[5]!=null){
			/*List<Integer> lessonIdList=lessonDAO.getLessonByName((String)o[5]);
			if(lessonIdList!=null && lessonIdList.size()>0){
				StringBuffer strBuf=new StringBuffer();
				for (int i = 0; i < lessonIdList.size(); i++) {
					Integer id=lessonIdList.get(i);
					strBuf.append(id);
					strBuf.append(",");
				}
				strBuf.deleteCharAt(strBuf.length() -1);
				paraVct.add(new SearchParam("lessonId",strBuf.toString(),SearchParam.SEARCH_PARAM_TYPE_IN));
			}*/
			paraVct.add(new SearchParam("lesson",o[5],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		if(o[6]!=null){
			User user=userDAO.getUserByTrueName((String)o[6]);
			if(user!=null){
				paraVct.add(new SearchParam("userId",user.getUid(),SearchParam.SEARCH_PARAM_TYPE_NUMBER));
			}
			//TODO 职员使用查询时，两个userId会冲突
		}
		//================20090620 yoo===========增加按电话查询===========
		if(o[7]!=null){
			paraVct.add(new SearchParam("tel",o[7],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		SearchParam[] para = new SearchParam[paraVct.size()];
		for(int i=0;i<paraVct.size();i++){
			para[i] = (SearchParam)paraVct.get(i);
		}
		return guestDAO.getGuest(sort, para, pageInfo);
	}
	
	
	/**
	 * Object[startTime,endTime,userId,guestName,schoolName,lessonName,usesrName];
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Contact> getContact(String sort,Object[] o, PageInfo pageInfo) {
		Vector paraVct = new Vector();
		if(o[0]!=null){
			paraVct.add(new SearchParam("logonDate",o[0],SearchParam.SEARCH_PARAM_TYPE_GREATER_OR_EQUAL));
		}
		if(o[1]!=null){
			paraVct.add(new SearchParam("logonDate",o[1],SearchParam.SEARCH_PARAM_TYPE_LESS_OR_EQUAL));
		}
		if(o[2]!=null){
			User user=userDAO.getUser((Integer)o[2]);
			if(user.getType() != 2){
				paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));
			}	
		}
		if(o[3]!=null){
			paraVct.add(new SearchParam("name",o[3],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		if(o[4]!=null){
			List<Integer> shoolIdList=schoolDAO.getSchoolByName((String)o[4]);
			if(shoolIdList!=null && shoolIdList.size()>0){
				StringBuffer strBuf=new StringBuffer();
				for (int i = 0; i < shoolIdList.size(); i++) {
					Integer id=shoolIdList.get(i);
					strBuf.append(id);
					strBuf.append(",");
				}
				strBuf.deleteCharAt(strBuf.length() -1);
				paraVct.add(new SearchParam("schoolId",strBuf.toString(),SearchParam.SEARCH_PARAM_TYPE_IN));
			}
		}
		if(o[5]!=null){
			paraVct.add(new SearchParam("lesson",o[5],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		if(o[6]!=null){
			User user=userDAO.getUser((String)o[6]);
			if(user!=null){
				paraVct.add(new SearchParam("userId",user.getUid(),SearchParam.SEARCH_PARAM_TYPE_NUMBER));
			}
			//TODO 职员使用查询时，两个userId会冲突
		}
		//================20090620 yoo===========增加按电话查询===========
		if(o[7]!=null){
			paraVct.add(new SearchParam("tel",o[7],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		SearchParam[] para = new SearchParam[paraVct.size()];
		for(int i=0;i<paraVct.size();i++){
			para[i] = (SearchParam)paraVct.get(i);
		}
		return contactDAO.getContact(sort, para, pageInfo);
	}
	
	/**
	 * 本周回访
	 * Object[startTime,endTime,userId,guestName,schoolName,lessonName];
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Guest> getWeekGuest(String sort,Object[] o, PageInfo pageInfo) {
		Vector paraVct = new Vector();
		if(o[0]!=null){
			paraVct.add(new SearchParam("returnVisitTime",o[0],SearchParam.SEARCH_PARAM_TYPE_GREATER_OR_EQUAL));
		}
		if(o[1]!=null){
			paraVct.add(new SearchParam("returnVisitTime",o[1],SearchParam.SEARCH_PARAM_TYPE_LESS_OR_EQUAL));
		}
		if(o[2]!=null){
			//-----------=======modify by yoo ========------------
			//回访类只看自己的
			/*
			User user=userDAO.getUser((Integer)o[2]);
			if(user.getType() != 2){
				paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));
			}
			*/
			paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));
		}
		if(o[3]!=null){
			paraVct.add(new SearchParam("name",o[3],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		if(o[4]!=null){
			List<Integer> shoolIdList=schoolDAO.getSchoolByName((String)o[4]);
			if(shoolIdList!=null && shoolIdList.size()>0){
				StringBuffer strBuf=new StringBuffer();
				for (int i = 0; i < shoolIdList.size(); i++) {
					Integer id=shoolIdList.get(i);
					strBuf.append(id);
					strBuf.append(",");
				}
				strBuf.deleteCharAt(strBuf.length() -1);
				paraVct.add(new SearchParam("schoolId",strBuf.toString(),SearchParam.SEARCH_PARAM_TYPE_IN));
			}
		}
		if(o[5]!=null){
			paraVct.add(new SearchParam("lesson",o[5],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		SearchParam[] para = new SearchParam[paraVct.size()];
		for(int i=0;i<paraVct.size();i++){
			para[i] = (SearchParam)paraVct.get(i);
		}
		return guestDAO.getGuest(sort, para, pageInfo);
	}
	
	/**
	 * 开课提醒
	 * Object[startTime,endTime,userId,guestName,schoolName,lessonName];
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Guest> getClassGuest(String sort,Object[] o, PageInfo pageInfo) {
		Vector paraVct = new Vector();
		if(o[0]!=null){
			paraVct.add(new SearchParam("startClassTime",o[0],SearchParam.SEARCH_PARAM_TYPE_GREATER_OR_EQUAL));
		}
		if(o[1]!=null){
			paraVct.add(new SearchParam("startClassTime",o[1],SearchParam.SEARCH_PARAM_TYPE_LESS_OR_EQUAL));
		}
		if(o[2]!=null){
			//-----------=======modify by yoo ========------------
			//回访类只看自己的
			/*
			User user=userDAO.getUser((Integer)o[2]);
			if(user.getType() != 2){
				paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));
			}
			*/
			paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));
		}
		if(o[3]!=null){
			paraVct.add(new SearchParam("name",o[3],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		if(o[4]!=null){
			List<Integer> shoolIdList=schoolDAO.getSchoolByName((String)o[4]);
			if(shoolIdList!=null && shoolIdList.size()>0){
				StringBuffer strBuf=new StringBuffer();
				for (int i = 0; i < shoolIdList.size(); i++) {
					Integer id=shoolIdList.get(i);
					strBuf.append(id);
					strBuf.append(",");
				}
				strBuf.deleteCharAt(strBuf.length() -1);
				paraVct.add(new SearchParam("schoolId",strBuf.toString(),SearchParam.SEARCH_PARAM_TYPE_IN));
			}
		}
		if(o[5]!=null){
			paraVct.add(new SearchParam("lesson",o[5],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		SearchParam[] para = new SearchParam[paraVct.size()];
		for(int i=0;i<paraVct.size();i++){
			para[i] = (SearchParam)paraVct.get(i);
		}
		return guestDAO.getGuest(sort, para, pageInfo);
	}
	
	/**
	 * 重要度回访
	 * Object[startTime,null,userId,guestName,schoolName,lessonName];
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Guest> getImportantTypeGuest(Object[] o, PageInfo pageInfo) {
		Vector paraVct = new Vector();
		if(o[0]!=null){
			paraVct.add(new SearchParam("startClassTime",o[0],SearchParam.SEARCH_PARAM_TYPE_GREATER_OR_EQUAL));
		}
		if(o[1]!=null){
			paraVct.add(new SearchParam("startClassTime",o[1],SearchParam.SEARCH_PARAM_TYPE_LESS_OR_EQUAL));
		}
		if(o[2]!=null){
			//-----------=======modify by yoo ========------------
			//回访类只看自己的
			/*
			User user=userDAO.getUser((Integer)o[2]);
			if(user.getType() != 2){
				paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));
			}
			*/
			paraVct.add(new SearchParam("userId",o[2],SearchParam.SEARCH_PARAM_TYPE_NUMBER));	
		}
		if(o[3]!=null){
			paraVct.add(new SearchParam("name",o[3],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		if(o[4]!=null){
			List<Integer> shoolIdList=schoolDAO.getSchoolByName((String)o[4]);
			if(shoolIdList!=null && shoolIdList.size()>0){
				StringBuffer strBuf=new StringBuffer();
				for (int i = 0; i < shoolIdList.size(); i++) {
					Integer id=shoolIdList.get(i);
					strBuf.append(id);
					strBuf.append(",");
				}
				strBuf.deleteCharAt(strBuf.length() -1);
				paraVct.add(new SearchParam("schoolId",strBuf.toString(),SearchParam.SEARCH_PARAM_TYPE_IN));
			}
		}
		if(o[5]!=null){
			paraVct.add(new SearchParam("lesson",o[5],SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT));
		}
		SearchParam[] para = new SearchParam[paraVct.size()];
		for(int i=0;i<paraVct.size();i++){
			para[i] = (SearchParam)paraVct.get(i);
		}
		return guestDAO.getGuest(para, pageInfo);
	}
	
	@Override
	public void updateGuest(Guest guest) {
		Integer signType = guest.getSignType();
		if(signType == 0){
//			已注册用户保存为Contact并删除guest
			Contact contact = new Contact(guest);
			contactDAO.saveContact(contact);
			guestDAO.deleteGuest(guest.getGid());
		}else if(signType == 2){
//		无效用户保存为InvalidGuest并删除guest
			InvalidGuest invalidGuest = new InvalidGuest(guest);
			contactDAO.saveInvalidGuest(invalidGuest);
			guestDAO.deleteGuest(guest.getGid());
		}else{
//		更新guest
			guestDAO.updateGuest(guest);
		}
	}

	public void setGuestDAO(GuestDAO guestDAO) {
		this.guestDAO = guestDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setSchoolDAO(SchoolDAO schoolDAO) {
		this.schoolDAO = schoolDAO;
	}

	public void setLessonDAO(LessonDAO lessonDAO) {
		this.lessonDAO = lessonDAO;
	}

	public void setContactDAO(ContactDAO contactDAO) {
		this.contactDAO = contactDAO;
	}

	@Override
	public List<Contact> listContact(int userId, String sort, PageInfo pageInfo) {
		User user=userDAO.getUser(userId);
		if(user.getType() == 2){
			return contactDAO.listContactForManager(sort, pageInfo);
		}else {
			return contactDAO.listContactForNormal(userId, sort, pageInfo);
		}
	}

	@Override
	public List<Guest> listGuestForDownload() {
		return guestDAO.listGuest();
	}

	@Override
	public Contact getContact(int id) {
		return contactDAO.getContact(new Integer(id));
	}

	@Override
	public List<Contact> getContactByTelephone(String tel) {
		//先找正式客户
		List<Contact> contactList =  contactDAO.listContactByTel(tel);
//		if(contactList!=null && contactList.size()>0){
//			return contactList;
//		}else{
		//再找联系人
		List<Guest> guestList =  guestDAO.listGuestByTel(tel);
		if(guestList!=null && guestList.size()>0){
			for (Guest guest : guestList) {
				Contact contact = new Contact(guest);
				contact.setCid(guest.getGid());//添加临时id供查看
				contact.setIsContact(false);//联系人，非客户
				contactList.add(contact);
			}
		}
		return contactList;
	}

}
